﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BarberShop
{
    public partial class AddClientForm : Form
    {
        private const string EditTitle = "Редагувати";
        private const string CreateTitle = "Створити";
        private const string Client = "клієнта";

        public string ClientName { get; set; }
        public string ClientSurname { get; set; }
        public string PrimaryPhone { get; set; }
        public string SecondaryPhone { get; set; }
        public string Notes { get; set; }

        public AddClientForm()
        {
            InitializeComponent();

            Text = CreateTitle + " " + Client;
            addButton.Text = CreateTitle;
        }

        public AddClientForm(String name, String surname, String primaryPhone, String secondaryPhone, String notes)
        {
            InitializeComponent();

            nameTextBox.Text = name;
            surnmaeTextBox.Text = surname;
            primaryPhoneTextBox.Text = primaryPhone;
            secondaryPhoneTextBox.Text = secondaryPhone;
            notesTextBox.Text = notes;

            Text = EditTitle + " " + Client;
            addButton.Text = EditTitle;
            addButton.Width = 94;
        }

        private void AddButtonClick(object sender, EventArgs e)
        {
            AddClient();
        }

        private void AddClient()
        {
            if (ValidateForm())
            {
                ClientName = nameTextBox.Text;
                ClientSurname = surnmaeTextBox.Text;
                PrimaryPhone = primaryPhoneTextBox.Text;
                SecondaryPhone = secondaryPhoneTextBox.Text;
                Notes = notesTextBox.Text;

                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void NameTextBoxValidating(object sender, CancelEventArgs e)
        {
            ValidateName();
        }


        private void PrimaryPhoneTextBoxValidating(object sender, CancelEventArgs e)
        {
            ValidatePhone();
        }


        private bool ValidateName()
        {
            if (nameTextBox.Text == "")
            {
                nameErrorProvider.SetError(nameTextBox, "Ім'я не може бути порожнім");
                return false;
            }
            return true;
        }

        private bool ValidatePhone()
        {
            if (primaryPhoneTextBox.Text == "")
            {
                nameErrorProvider.SetError(primaryPhoneTextBox, "Телефон не може бути порожнім");
                return false;
            }
            return true;
        }

        private bool ValidateForm()
        {
            return ValidateName() && ValidatePhone();
        }

        private void CloseButtonLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void FilterPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 13: //Enter key
                    AddClient();
                    break;
                case 27: //Esc key
                    Close();
                    break;
            }
        }
    }
}
